<?php
require_once "lzu_op_fns.php";
session_start();
if(!check_valid_user()){
    header("Location: login.php");
    exit;
}
$uid = $_SESSION['uid'];
require_once('db_fns.php');
$conn = db_connect();

$oid = $conn->query("select fn_create_order({$uid})")->fetch_row()[0];

if (isset($_SESSION['flag']) && $_SESSION['flag'] != 0) {
    foreach ($_SESSION as $key => $did) {
        if ($key != 'uid' && $key != 'valid_user' && $key != 'flag') {
            $conn->query("call sp_order_dishes({$oid},{$did},1)");
            unset($_SESSION["d" . $did]);
        }
    }
    $_SESSION['flag'] = 0;
    echo "<script>alert('提交成功！');location.href='index.php';</script>";
} else {
    echo "<script>alert('您的购物车是空的，请先选择菜品！');location.href='index.php';</script>";
}

